using FireStarterModeller.BO;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    [TestFixture]
    public class TestUIInfo : TestBase
    {
        [SetUp]
        public override void SetupTest()
        {
            //Runs every time that any testmethod is executed
            base.SetupTest();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
        }

        [TearDown]
        public override void TearDownTest()
        {
            //runs every time any testmethod is complete
            base.TearDownTest();
        }

        [Test]
        public void Test_InsertNewUIInfo_Invalid()
        {
            //---------------Set up test pack-------------------
            UIView uiView = new UIView();

            //---------------Assert Precondition----------------
            Assert.IsTrue(uiView.Status.IsNew);

            //---------------Execute Test ----------------------
            try
            {
                uiView.Name = null;
                uiView.Save();
                Assert.Fail("expected Err");
            }
            //---------------Test Result -----------------------
            catch (BusObjectInAnInvalidStateException ex)
            {
                StringAssert.Contains("Name' is a compulsory field and has no value", ex.Message);
            }
            Assert.IsTrue(uiView.Status.IsNew);
        }

        [Test]
        public void Test_InsertNewUIInfo_DefaultValues()
        {
            //---------------Set up test pack-------------------

            //---------------Execute Test ----------------------
            UIView uiView = new UIView();

            //---------------Test Result -----------------------
            Assert.AreEqual("default", uiView.Name);
        }

        [Test]
        public void Test_InsertNewUIInfo()
        {
            //---------------Set up test pack-------------------
            UIView uiView = TestUtils.CreateUnsavedValidUIInfo();

            //---------------Assert Precondition----------------
            Assert.IsTrue(uiView.Status.IsNew);

            //---------------Execute Test ----------------------
            uiView.Save();

            //---------------Test Result -----------------------
            Assert.IsFalse(uiView.Status.IsNew);
        }

        [Test]
        public void Test_DeleteUIInfo()
        {
            //---------------Set up test pack-------------------
            UIView uiView = TestUtils.CreateSavedUIInfo();

            //---------------Assert Precondition----------------
            Assert.IsFalse(uiView.Status.IsNew);
            string message;
            Assert.IsTrue(uiView.IsDeletable(out message));

            //---------------Execute Test ----------------------
            uiView.MarkForDelete();
            uiView.Save();

            //---------------Test Result -----------------------  
            Assert.IsTrue(uiView.Status.IsNew);
            Assert.IsTrue(uiView.Status.IsDeleted);
        }

        [Test]
        public void Test_UpdateUIInfo()
        {
            //---------------Set up test pack-------------------
            UIView uiView = TestUtils.CreateSavedUIInfo();

            //---------------Assert Precondition----------------
            Assert.IsFalse(uiView.Status.IsNew);
            string message;
            Assert.IsTrue(uiView.IsEditable(out message));

            //---------------Execute Test ----------------------
            uiView.Name = "Test Form";

            //---------------Test Result -----------------------
            Assert.IsFalse(uiView.Status.IsNew);
        }


        [Test]
        public void Test_DeleteUIInfo_DeletesFormLayout()
        {
            //---------------Set up test pack-------------------
            UIView uiView = TestUtils.CreateSavedUIInfo();
            UIFormInfo uiFormInfo = TestUtils.CreateUnsavedValidUIFormInfo(uiView);
            uiFormInfo.Save();
            //---------------Assert Precondition----------------
            Assert.IsFalse(uiView.Status.IsNew);
            string message;
            Assert.IsTrue(uiView.IsEditable(out message));

            //---------------Execute Test ----------------------
            uiView.MarkForDelete();
            uiView.Save();

            //---------------Test Result -----------------------
            Assert.IsTrue(uiView.Status.IsNew);
            Assert.IsTrue(uiView.Status.IsDeleted);
            Assert.IsTrue(uiFormInfo.Status.IsDeleted);
        }


        [Test]
        public void Test_DeleteUIInfo_DeletesGridInfo()
        {
            //---------------Set up test pack-------------------
            UIView uiView = TestUtils.CreateSavedUIInfo();
            UIGridInfo uiGridInfo = TestUtils.CreateUnsavedValidUIGridInfo(uiView);
            uiGridInfo.Save();
            //---------------Assert Precondition----------------
            Assert.IsFalse(uiView.Status.IsNew);
            string message;
            Assert.IsTrue(uiView.IsEditable(out message));

            //---------------Execute Test ----------------------
            uiView.MarkForDelete();
            uiView.Save();

            //---------------Test Result -----------------------
            Assert.IsTrue(uiView.Status.IsNew);
            Assert.IsTrue(uiView.Status.IsDeleted);
            Assert.IsTrue(uiGridInfo.Status.IsDeleted);
        }

    }
}